package com.bite.login.controller;

import com.bite.login.entity.User;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@CrossOrigin
@RestController
public class UserController {

    private Map<String, String> userDB = new HashMap<>();

    @RestController
    public class TestController {

        @GetMapping("/test")
        public String hello() {
            return "OK";
        }
    }

    @PostMapping("/api/register")
    public ResponseEntity<String> register(@RequestBody User user) {
        if (userDB.containsKey(user.getUsername())) {
            return ResponseEntity.badRequest().body("用户已存在");
        }
        userDB.put(user.getUsername(), user.getPassword());
        return ResponseEntity.ok("注册成功");
    }

    @PostMapping("/api/login")
    public ResponseEntity<String> login(@RequestBody User user) {
        String storedPassword = userDB.get(user.getUsername());
        if (storedPassword == null) {
            return ResponseEntity.badRequest().body("用户不存在");
        }
        if (!storedPassword.equals(user.getPassword())) {
            return ResponseEntity.badRequest().body("密码错误");
        }
        return ResponseEntity.ok("登录成功");
    }
}
